package it.prova.elementedit;

import it.prova.Consts;
import it.prova.Utility;
import it.prova.action.MultiDataUnitIterateAction;
import it.prova.drop.IterateDropCustomizer;
import it.prova.drop.MultiDataUnitDropCustomizer;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jst.jsf.common.dom.TagIdentifier;
import org.eclipse.jst.pagedesigner.editors.PageDesignerActionConstants;
import org.eclipse.jst.pagedesigner.editors.palette.IDropSourceData;
import org.eclipse.jst.pagedesigner.elementedit.AbstractElementEdit;
import org.eclipse.jst.pagedesigner.itemcreation.ITagCreator;
import org.eclipse.jst.pagedesigner.itemcreation.customizer.IDropCustomizer;
import org.eclipse.jst.pagedesigner.parts.ElementEditPart;
import org.eclipse.jst.pagedesigner.parts.NodeEditPart;
import org.eclipse.jst.pagedesigner.viewer.IDropLocationStrategy;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
import org.w3c.dom.Element;

@SuppressWarnings("restriction")
public class MultiDataUnitIterateElementEdit extends AbstractElementEdit
{
	private MultiDataUnitIterateAction action;
	
	private String tagName;
	
	public MultiDataUnitIterateElementEdit(String tagName)
	{
		super();
		
		this.tagName = tagName;
	}
	
	@Override
	public void createEditPolicies(ElementEditPart part)
	{
//		part.installEditPolicy
//		(
//			ItemCreationRequest.REQ_ITEM_CREATION,
//			new MyElementCreationEditPolicy(part)
//		);
	}

	@Override
	public boolean handleModelChange
	(
		Element ele, 
		ElementEditPart part, 
		boolean recursive
	)
	{
		List<Element> elements = new ArrayList<Element>();
//		Utility.findAllElementsByTagName(ele.getOwnerDocument().getDocumentElement(), ele.getLocalName(), elements);
		Utility.findAllElements(ele.getOwnerDocument().getDocumentElement(), elements);
		
		for(Element e: elements)
		{
			if((!e.equals(ele)) && (e instanceof IDOMElement))
			{
				IDOMElement el = (IDOMElement) e;
				EditPart editPart = (EditPart) el.getAdapterFor(EditPart.class);
				
				if(editPart != null)
				{
					editPart.refresh();
				}
			}
		}
		return true;
	}

	@Override
	public void fillContextMenu(IMenuManager contextMenu, Element ele)
	{
		super.fillContextMenu(contextMenu, ele);
		
		MultiDataUnitIterateAction action = getAction();

		action.setElement(ele);
		
        contextMenu.appendToGroup
        (
        	PageDesignerActionConstants.GROUP_SPECIAL, 
        	action
        );
	}
	
	private MultiDataUnitIterateAction getAction()
	{
		if (action == null)
		{
			action = new MultiDataUnitIterateAction();
		}
		return action;
	}

	@Override
	public boolean fillContainerContextMenu
	(
		IMenuManager contextMenu,
		ElementEditPart elePart, 
		NodeEditPart nodePart,
		ISelection innerSelection
	)
	{
		return super.fillContainerContextMenu
		(
			contextMenu, 
			elePart, 
			nodePart,
			innerSelection
		);
	}

	@Override
	public boolean isResizable(Element ele)
	{
		return super.isResizable(ele);
	}

	@Override
	public IDropLocationStrategy getDropRequestorLocationStrategy
	(
		TagIdentifier tag, 
		EditPartViewer viewer
	)
	{
		return super.getDropRequestorLocationStrategy(tag, viewer);
	}

	@Override
	public IDropCustomizer getDropCustomizer(IDropSourceData dropSourceData)
	{
//		return new MyDropCustomizer(dropSourceData);
		if
		(
			Consts.TAG_VALUE_NAME.equals(tagName) || 
			Consts.TAG_LABEL_NAME.equals(tagName)
		)
		{
			return new MultiDataUnitDropCustomizer(tagName);
		}
		else if(Consts.TAG_ITERATE_NAME.equals(tagName))
		{
			return new IterateDropCustomizer(tagName);
		}
		return null;
	}

	@Override
	public ITagCreator getTagCreator(TagIdentifier tagId)
	{
//		return new MyTagCreator(tagId);
		return new MyTagCreator();
//		return super.getTagCreator(tagId);
	}	
}